package com.epichust.bootumpp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.epichust.bootumpp.model.bo.FiltersBO;
import com.epichust.bootumpp.model.dao.Mbb5OrderBomDao;
import com.epichust.bootumpp.model.entity.Mbb5OrderBom;
import com.epichust.bootumpp.service.IMbb5OrderBomService;
import com.epichust.bootumpp.utils.CommonUtils;
import com.epichust.bootumpp.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：longyang
 * @date ：Created in 2021/1/25 14:43
 * @description：订单bomService
 */
@Service
@Transactional
public class Mbb5OrderBomServiceImpl implements IMbb5OrderBomService {

    @Autowired
    private Mbb5OrderBomDao mbb5OrderBomDao;

    /**
     * 分页查询订单bom信息
     * @param map
     * @return
     */
    @Override
    public List<Mbb5OrderBom> getAllMbb5OrderBom(Map<String, Object> map) throws Exception{
        //处理分页信息
        if(map.get("rows") != null && map.get("page") != null){
            int rows = Integer.parseInt(map.get("rows").toString());
            int page = Integer.parseInt(map.get("page").toString());
            int firstLimit = (page-1)*rows;
            int lastLimit = page*rows;
            map.put("firstLimit",firstLimit);
            map.put("lastLimit",lastLimit);
        }
        //处理前端查询信息
        if(map.get("filters") != null){
            String filtersStr = map.get("filters").toString();
            List<FiltersBO> filters = GsonUtils.parseString2List(filtersStr,FiltersBO.class);
            for(FiltersBO bo :filters){
                map.put(bo.getField(),bo.getData());
            }
        }
        String records = mbb5OrderBomDao.getAllMbb5OrderBomRecords(map);
        List<Mbb5OrderBom> list = mbb5OrderBomDao.getAllMbb5OrderBom(map);
        if(!list.isEmpty()){
            list.get(0).setRecords(records);
        }
        return list;
    }

    /**
     * 新增订单bom信息
     * @param mbb5OrderBom
     */
    @Override
    public void insertMbb5OrderBom(Mbb5OrderBom mbb5OrderBom) {
        mbb5OrderBom.setCreateDate(new Date());
        mbb5OrderBom.setModifyDate(new Date());
        mbb5OrderBom.setCreateId(StringUtils.isEmpty(mbb5OrderBom.getCreateId()) ? "SYSTEM" : mbb5OrderBom.getCreateId());
        mbb5OrderBom.setModifyId(mbb5OrderBom.getCreateId());
        mbb5OrderBom.setSiteCode(StringUtils.isEmpty(mbb5OrderBom.getSiteCode()) ? "DEFUALT" : mbb5OrderBom.getSiteCode());
        mbb5OrderBom.setWorkCenterCode(StringUtils.isEmpty(mbb5OrderBom.getWorkCenterCode()) ? "DEFUALT" : mbb5OrderBom.getWorkCenterCode());
        mbb5OrderBomDao.insertMbb5OrderBom(mbb5OrderBom);
    }

    /**
     * 修改订单bom信息
     * @param mbb5OrderBom
     */
    @Override
    public void updateMbb5OrderBom(Mbb5OrderBom mbb5OrderBom) {
        mbb5OrderBom.setModifyDate(new Date());
        mbb5OrderBomDao.updateMbb5OrderBom(mbb5OrderBom);
    }
}
